﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using OAuthClient.Properties;
using ComponentFactory.Krypton.Toolkit;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace OAuthClient
{
    /// <summary>
    /// 
    /// </summary>
    public partial class XmlForm : KryptonForm
    {
        /// <summary>
        /// 
        /// </summary>
        public XmlForm()
        {
            InitializeComponent();
            _contentPanel.Navigate("about:blank");
            _paramsGridView.CellFormatting += OnParamsGridViewCellFormatting;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnParamsGridViewCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // compare the column to the column you want to format
            if (_paramsGridView.Columns[e.ColumnIndex] == _keyDgvColumn)
            {
                var dr = (_paramsGridView.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                e.CellStyle.BackColor =
                    string.Compare(dr["header"].ToString(), "1", false) == 0 ?
                        Color.LemonChiffon : SystemColors.Window;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public XmlForm(string content)
            : this()
        {
            DisplayHtml(content);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        public XmlForm(XmlDocument doc)
            : this()
        {
            DisplayHtml(doc);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        public XmlForm(LogViewItem item)
            : this()
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("order"));
            dt.Columns.Add(new DataColumn("key"));
            dt.Columns.Add(new DataColumn("content"));
            dt.Columns.Add(new DataColumn("header"));
            _paramsGridView.AutoGenerateColumns = false;
            _paramsGridView.DataSource = dt;

            DisplayHtml(item.Message);
            Array.ForEach(item.Headers.AllKeys, key =>
            {
                var dr = dt.NewRow();
                dr["order"] = (dt.Rows.Count + 1).ToString();
                dr["key"] = key;
                dr["content"] = item.Headers[key];
                dr["header"] = 1;
                dt.Rows.Add(dr);
            });
        }

        /// <summary>
        ///     
        /// </summary>
        private void DisplayHtml(string message)
        {
            _contentPanel.Navigate("about:blank");

            if (_contentPanel.Document != null)
            {
                _contentPanel.Document.Write(string.Empty);
            }
            var xslt = new XslCompiledTransform();

            using (var stm = new MemoryStream(new UTF8Encoding().GetBytes(
                Resources.ResourceManager.GetObject("xml_pretty_print") as string)))
            {
                xslt.Load(new XmlTextReader(stm));
                var doc = new XmlDocument();
                doc.LoadXml(message);
                using (var sw = new StringWriter())
                {
                    xslt.Transform(doc, new XmlTextWriter(sw));
                    _contentPanel.DocumentText = sw.GetStringBuilder().ToString();
                }
            }
        }

        /// <summary>
        ///     
        /// </summary>
        private void DisplayHtml(XmlDocument doc)
        {
            _contentPanel.Navigate("about:blank");

            if (_contentPanel.Document != null)
            {
                _contentPanel.Document.Write(string.Empty);
            }
            var xslt = new XslCompiledTransform();

            using (var stm = new MemoryStream(new UTF8Encoding().GetBytes(
                Resources.ResourceManager.GetObject("xml_pretty_print") as string)))
            {
                xslt.Load(new XmlTextReader(stm));
                using (var sw = new StringWriter())
                {
                    xslt.Transform(doc, new XmlTextWriter(sw));
                    _contentPanel.DocumentText = sw.GetStringBuilder().ToString();
                }
            }
        }
    }
}
